package android.zhcs.myapplication.ui.gallery;

import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.widget.Toast;
import android.zhcs.myapplication.Cac;
import android.zhcs.myapplication.R;
import android.zhcs.myapplication.bean2;
import android.zhcs.myapplication.beanfh;
import android.zhcs.myapplication.databinding.FragmentGalleryBinding;
import android.zhcs.myapplication.databinding.ItemBinding;
import android.zhcs.myapplication.databinding.ItemtcBinding;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;
    private FragmentGalleryBinding binding;
    int a = 1;
    private ArrayList<bean1> list = new ArrayList<>();
    private Timer timer;

    @Override
    public void onDestroy() {
        timer.cancel();
        super.onDestroy();
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                new ViewModelProvider(this).get(GalleryViewModel.class);

        binding = FragmentGalleryBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                if (a == 1) {
                    binding.hld.setImageResource(R.drawable.hong);
                    a = 2;
                } else if (a == 2) {
                    binding.hld.setImageResource(R.drawable.lv);
                    a = 3;
                } else if (a == 3) {
                    binding.hld.setImageResource(R.drawable.huang);
                    a = 1;
                }
            }
        };
        timer.schedule(timerTask, 0, 3000);

        wlqqhq();
        binding.xzx.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String s = adapterView.getItemAtPosition(i).toString();
                if (s.equals("路口升序")) {
                    Collections.sort(list, new Comparator<bean1>() {
                        @Override
                        public int compare(bean1 bean1, bean1 t1) {
                            return bean1.id - t1.id;
                        }
                    });
                } else if (s.equals("路口降序")) {
                    Collections.sort(list, new Comparator<bean1>() {
                        @Override
                        public int compare(bean1 bean1, bean1 t1) {
                            return t1.id - bean1.id;
                        }
                    });
                } else if (s.equals("红灯升序")) {
                    Collections.sort(list, new Comparator<bean1>() {
                        @Override
                        public int compare(bean1 bean1, bean1 t1) {
                            return bean1.hong - t1.hong;
                        }
                    });
                } else if (s.equals("红灯降序")) {
                    Collections.sort(list, new Comparator<bean1>() {
                        @Override
                        public int compare(bean1 bean1, bean1 t1) {
                            return t1.hong - bean1.hong;
                        }
                    });
                }
                getlist();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        binding.plsz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<Integer> list2 = new ArrayList<>();
                list.forEach(bean1 -> {
                    if (bean1.xz) {
                        list2.add(bean1.id);
                    }
                });
                if(list2.size() != 0){
                    tcc(list2);
                }else{
                    Toast.makeText(getContext(),"请选择路口",Toast.LENGTH_LONG).show();
                }
            }
        });
        return root;
    }
    public void wlqqhq() {
        list = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            int finalI = i;
            Cax.get("/api/v2/get_trafficlight_config", "POST", "{\"TrafficLightId\":" + i + ", \"UserName\":\"user1\"}", new Cax<bean2>() {
                @Override
                protected void bix(bean2 bean) {
                    list.add(new bean1(finalI, bean.RedTime, bean.GreenTime, bean.YellowTime, false));
                    Collections.sort(list, new Comparator<bean1>() {
                        @Override
                        public int compare(bean1 bean1, bean1 t1) {
                            return bean1.id - t1.id;
                        }
                    });
                    getlist();
                }
            });
        }
    }
    public void tcc(ArrayList<Integer> s) {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.itemtc, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        ItemtcBinding bind = ItemtcBinding.bind(inflate);
        builder.setView(bind.getRoot());
        AlertDialog dialog = builder.create();
        dialog.show();
        bind.qx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        bind.qr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bind.hong.getText().toString().equals("") || bind.huang.getText().toString().equals("") || bind.lv.getText().toString().equals("")) {
                    Toast.makeText(getContext(),"请输入设置时长",Toast.LENGTH_SHORT).show();
                } else {
                    if (Integer.parseInt(bind.hong.getText().toString()) > 99 || Integer.parseInt(bind.hong.getText().toString()) < 1) {
                        Toast.makeText(getContext(),"请输入符合要求的红灯时间",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (Integer.parseInt(bind.huang.getText().toString()) > 5 || Integer.parseInt(bind.huang.getText().toString()) < 3) {
                        Toast.makeText(getContext(),"请输入符合要求的黄灯时间",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (Integer.parseInt(bind.lv.getText().toString()) > 99 || Integer.parseInt(bind.lv.getText().toString()) < 1) {
                        Toast.makeText(getContext(),"请输入符合要求的绿灯时间",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    s.forEach(item ->{
                        Cax.get("/api/v2/set_trafficlight_config", "POST", "{\"TrafficLightId\":" + item +",\"RedTime\":" +Integer.parseInt(bind.hong.getText().toString()) +",\"GreenTime\":" + Integer.parseInt(bind.lv.getText().toString()) +",\"YellowTime\":" +Integer.parseInt(bind.huang.getText().toString()) +"," + "\"UserName\":\"user1\"}", new Cax<beanfh>() {
                            @Override
                            protected void bix(beanfh bean) {

                            }
                        });
                    });
                    Toast.makeText(getContext(), "设置成功", Toast.LENGTH_SHORT).show();
                    wlqqhq();
                    dialog.dismiss();
                }
            }
        });
    }
    public void tc(int s) {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.itemtc, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        ItemtcBinding bind = ItemtcBinding.bind(inflate);
        builder.setView(bind.getRoot());
        AlertDialog dialog = builder.create();
        dialog.show();
        bind.qx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        bind.qr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bind.hong.getText().toString().equals("") || bind.huang.getText().toString().equals("") || bind.lv.getText().toString().equals("")) {
                    Toast.makeText(getContext(),"请输入设置时长",Toast.LENGTH_SHORT).show();
                } else {
                    if (Integer.parseInt(bind.hong.getText().toString()) > 99 || Integer.parseInt(bind.hong.getText().toString()) < 1) {
                        Toast.makeText(getContext(),"请输入符合要求的红灯时间",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (Integer.parseInt(bind.huang.getText().toString()) > 5 || Integer.parseInt(bind.huang.getText().toString()) < 3) {
                        Toast.makeText(getContext(),"请输入符合要求的黄灯时间",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (Integer.parseInt(bind.lv.getText().toString()) > 99 || Integer.parseInt(bind.lv.getText().toString()) < 1) {
                        Toast.makeText(getContext(),"请输入符合要求的绿灯时间",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Cax.get("/api/v2/set_trafficlight_config", "POST", "{\"TrafficLightId\":" + s +",\"RedTime\":" +Integer.parseInt(bind.hong.getText().toString()) +",\"GreenTime\":" + Integer.parseInt(bind.lv.getText().toString()) +",\"YellowTime\":" +Integer.parseInt(bind.huang.getText().toString()) +"," + "\"UserName\":\"user1\"}", new Cax<beanfh>() {
                        @Override
                        protected void bix(beanfh bean) {
                            if (bean.RESULT.equals("S")) {
                                Toast.makeText(getContext(), "设置成功", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                                wlqqhq();
                            }else{
                                Toast.makeText(getContext(),"设置失败",Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        }
                    });
                }
            }
        });
    }

    private void getlist() {
        Log.e("TAG", "getlist: 123321321");
        binding.lb.setAdater(1, list.size(), R.layout.item, new Cac() {
            @Override
            public void bix(View view, int position) {
                Log.e("TAG数组长度", position+"");
                ItemBinding bind = ItemBinding.bind(view);
                bean1 bean1 = list.get(position);
                if (position == 0) {
                    bind.xs1.setVisibility(View.VISIBLE);
                    bind.xs2.setVisibility(View.VISIBLE);
                    bind.xs3.setVisibility(View.VISIBLE);
                    bind.xs4.setVisibility(View.VISIBLE);
                    bind.xs5.setVisibility(View.VISIBLE);
                    bind.xs6.setVisibility(View.VISIBLE);
                }
                bind.id.setText(bean1.id + "");
                bind.hong.setText(bean1.hong + "");
                bind.lv.setText(bean1.lv + "");
                bind.huang.setText(bean1.huang + "");
                bind.xz.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if(b){
                            bean1.xz = true;
                        }else{
                            bean1.xz = false;
                        }
                        Log.d("TAG", list.get(position).toString());
                    }
                });
                bind.sz.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        tc(position+1);
                    }
                });
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}